<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/treetable/jquery.treetable.css"/>
    <link rel="stylesheet" href="/css/treetable/jquery.treetable.theme.default.css"/>
    <script src="/js/libs/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
    <script src="/js/bootstrap/bootstrap.min.js"></script>
    <script src="/js/bootstrap/bootstrap-select.min.js"></script>
    <script src="/js/jq.js"></script>
    <script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/layui/layui.js"></script>
    <script src="/js/dict.js"></script>
    <script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
    <script src="/js/my/ztree-menu.js"></script>
    <script src="/js/my/permission.js"></script>
    <script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
    <script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
    <script src="/js/libs/jquery.treetable.js"></script>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-md-offset-2 col-lg-offset-2">
    <form class="form-horizontal" onsubmit="return false" id="form">
        <fieldset>
            <input type="hidden" id="id" name="id">
            <input type="hidden" id="projectId" name="projectId">
            <div class='form-group'>
                <label class='col-md-4 control-label'>执行者：</label>
                <div class='col-md-8'>
                    <input class='form-control' type='hidden' name='participantId' id='participantId'>
                    <input style="width: 90%;float: left;" class='form-control' placeholder='执行者' type='text'
                           name='participantName' id='participantName'
                           readonly>
                    <button style="width: 10%;float: right; height: 32px; line-height: 32px;"
                            class="layui-btn layui-btn-sm"
                            onclick="showTableAndFlushFields( 'user', 'user', '用户', true, false, 'participantId:id', 'participantName:nickname')">
                        <i class="layui-icon">&#xe615;</i>
                    </button>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>任务描述：</label>
                <div class='col-md-8'>
                    <textarea class="form-control" placeholder="任务描述" name="description" id="description"
                              data-bv-notempty='true' data-bv-notempty-message='任务描述 不能为空'></textarea>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>截止日期</label>
                <div class='col-md-8'>
                    <input class='form-control' placeholder='截止日期' type='text' name='deadline' id='deadline'
                           data-bv-notempty='true' data-bv-notempty-message='截止日期 不能为空'>
                </div>
            </div>
            <div class='form-group'>
                <label class='col-md-4 control-label'>任务状态:</label>
                <div class='col-md-8'>
                    <select class="form-control input-sm selectpicker show-tick" name="status" id="status"
                            title="任务状态" data-live-search="true"></select>
                </div>
            </div>
            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" type="submit" onclick="save()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>
</div>

<script type="text/javascript">
    $(function () {
        $('#form').bootstrapValidator();
    });

    layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;

        laydate.render({
            elem: '#deadline'
        });
    });

    var status = showDictSelect('status', 'task_status', '', '1');
    initData();

    function initData() {
        var id = getUrlParam("id");
        if (id != "") {
            $.ajax({
                type: 'get',
                url: '/task/' + id,
                async: false,
                success: function (json) {
                    if (json.code === 0) {
                        var data = json.data;
                        $('#id').val(data.id);
                        $('#projectId').val(data.projectId);
                        $('#participantId').val(data.participantId);
                        $('#participantName').val(data.participantName);
                        $('#description').val(data.description);
                        $('#deadline').val(data.deadline);
                        $('#status').val(data.status);
                    }
                }
            });
        }
    }

    function save() {
        $('#form').bootstrapValidator();
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }

        var formdata = $("#form").serializeObject();

        $.ajax({
            type: 'put',
            url: '/task',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata),
            success: function (data) {
                layer.msg("修改成功", {shift: -1, time: 1000}, function () {
                    parent.location.reload();
                });
            }
        });
    }

</script>
</body>
</html>
